﻿ActiveTrackManager = function( targetDiv, filteredTrackManager )
{
    this.m_activeTracks = {};
    this.m_$targetDiv = $(targetDiv);
    this.m_filteredTrackManager = filteredTrackManager;
    this.m_count = 0;  //number of active tracks
    
    this.onFilteredTrackChanged = function( event, trackID, trackState )
    {
        if ( trackState == TrackManager.STATUS_REMOVED && trackID in this.m_activeTracks )
        {
            delete m_activeTracks[ trackID ];
            this.ShowSelectedTracksInfo();
            $(this).trigger('onchange', [trackID, trackState] );
        }
        
        if ( trackID == null )
        {
            var elements2delete = [];
            var changed = true;
            for ( var k in this.m_activeTracks )
                if ( !this.m_filteredTrackManager.isTrackFiltered( k ) )
                    elements2delete.push(k);
            if ( elements2delete.length )
            {
                for (var k = 0; k < elements2delete.length; k++)
                    delete this.m_activeTracks[elements2delete[k]];
                    
                this.m_count -= elements2delete.length;
                
                this.ShowSelectedTracksInfo();
                $(this).trigger('onchange', [null, null]);
            }
        }
    }

    $(this.m_filteredTrackManager).bind( 'onchange', $.proxy( this, 'onFilteredTrackChanged' ) );
}

ActiveTrackManager.prototype.activateTracks = function( trackIDs )
{
    this.m_activeTracks = {};
    this.m_count = trackIDs.length;
    
    for ( var curTrackID = 0; curTrackID < trackIDs.length; curTrackID++ )
    {
        var track = this.m_filteredTrackManager.getTrack( trackIDs[curTrackID] );
        if ( track )
            this.m_activeTracks[ trackIDs[curTrackID] ] = track;
    }
    
    $(this).trigger('onchange', [null, null]);
    
    this.ShowSelectedTracksInfo();
}

ActiveTrackManager.prototype.ShowSelectedTracksInfo = function()
{
    this.m_$targetDiv.empty();

    if ( !this.m_count ) return;
    
    var $table = $('<table>').addClass('active_track_table');
                    // .append($("<caption>Выбранные на карте треки</caption>"));
                    
    $table.append($('<th>').text('i'))
          .append($('<th>').text('Описание'))
          .append($('<th>').text('Скачать'))
          .append($('<th>').text('Внешняя ссылка'))
          .append($('<th>').text('Теги'))
          .append($('<th>').text('Длина'));
    this.m_$targetDiv.append($table);
          
    for ( var curTrackID in this.m_activeTracks )
    {
        var $infoButton = $('<input>')
            .attr({
                type:     'button',
                value:    'Info'})
            .bind('click', function(){
                var infoWidget = new TrackInfoWidget();
                infoWidget.show( this.track_id );
            });
        $infoButton.get(0).track_id = curTrackID;
            
        var $infoTD = $('<td>').append( $('<form>').append($infoButton) );
        
        var curTrack = this.m_filteredTrackManager.getTrack( curTrackID );        
        assert( curTrack, "Invalid track ID: " + curTrackID );
        
        var tag_names = curTrack.m_tags.map( function(tag_id) { return gTags[tag_id] } ); 
        
        var $link_td = $('<td>')
                          .append($('<a>').text("копия").attr({ href: "tracks/"+curTrack.filename, target: "parent" }))
                          .attr({align:'center'});
                          
        if ( curTrack.src_url )
            $link_td
                .prepend($('<br/>'))
                .prepend($('<a>').text("оригинал").attr({ href : curTrack.src_url, target: "parent" }));
        
        var $external_td = $('<td>').attr({align:'center'});
        if (curTrack.url_description) 
            $external_td.append($('<a>').text("link").attr({href:curTrack.url_description, target: "parent"}))
        else
            $external_td.text("Нет");
        
        var $tr = $('<tr>');
        $tr.append($infoTD)
           .append($('<td>').text(curTrack.short_description))
           .append($link_td)
           .append($external_td)
           .append($('<td>').text(tag_names.join(', ')))
           .append($('<td>').text((curTrack.track_length/1000).toFixed(1) + " км"));
        $table.append($tr);
    }
}

ActiveTrackManager.prototype.isTrackActive = function( trackID )
{
    return (trackID in this.m_activeTracks );
}